17D - Notepad - CodeForces Solution


number theory *2400

Please click on ads to support us..

C++ Code:

#include<bits/stdc++.h>

using namespace std;

const int mx=200005;

typedef long long ll;

ll pow(ll a,ll b,ll m)
{
    ll ans=1ll;
    while(b)
    {
        if(b&1ll) ans=(ans*a)%m;
        a=(a*a)%m;
        b=b>>1ll;
    }
    return (ans);
}

/// totient function
ll phi(ll n)
{
    ll result = n;
    for (ll i = 2; i * i <= n; i++) {
        if (n % i == 0) {
            while (n % i == 0)
                n /= i;
            result -= result / i;
        }
    }
    if (n > 1)
        result -= result / n;

    return result;
}

int main()

{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    cout.tie(NULL);

    ll c,Phi;
    string s1,s2;

    cin>>s1>>s2>>c;

    Phi=c;
    ll b=0ll;
    ll n_1=0ll;

    /// finding b%c
    for(int i=0;i<(int)s1.size();i++)
    {
        b=b*10+(s1[i]-'0');
        b%=c;
    }
    /// make string of n-1
    for(int i=(int)s2.size()-1;i>=0;i--)
    {
        if(s2[i]=='0') s2[i]='9';
        else
        {
            s2[i]--;
            break;
        }
    }

    Phi=phi(c);
    bool flag=0;

    /// taking number (n-1)
    for(int i=0;i<(int)s2.size();i++)
    {
        n_1=n_1*10+(s2[i]-'0');
        if(n_1>=Phi) flag=1;
        n_1%=Phi;
    }

    if(flag) n_1+=Phi;
    ll ans=((b-1ll)*pow(b,n_1,c))%c;
    if(ans<0) ans=(ans+c)%c;

    if(ans) cout<<ans<<endl;
    else cout<<c<<endl;

	return 0;
}
   			 					      	 			    	  	


Comments

Submit
0 Comments
More Questions

1325A - EhAb AnD gCd
746A - Compote
318A - Even Odds
550B - Preparing Olympiad
939B - Hamster Farm
732A - Buy a Shovel
1220C - Substring Game in the Lesson
452A - Eevee
1647B - Madoka and the Elegant Gift
1408A - Circle Coloring
766B - Mahmoud and a Triangle
1618C - Paint the Array
469A - I Wanna Be the Guy
1294A - Collecting Coins
1227A - Math Problem
349A - Cinema Line
47A - Triangular numbers
1516B - AGAGA XOOORRR
1515A - Phoenix and Gold
1515B - Phoenix and Puzzle
155A - I_love_username
49A - Sleuth
1541A - Pretty Permutations
1632C - Strange Test
673A - Bear and Game
276A - Lunch Rush
1205A - Almost Equal
1020B - Badge
1353A - Most Unstable Array
770A - New Password